/*
 *下载文件
 */
export const downloadFile = (url, fileTyp) => {
  url = "https://" + document.location.host + url
  console.log("url", url)
  // console.log({
  //   url : '/pages/oaupload/oaupload?url=' + encodeURIComponent(url + "&wx=1")
  // })
  if (window.__wxjs_environment === "miniprogram") {
    url += "&wx=1"
    url = encodeURIComponent(url)
    wx.miniProgram.navigateTo({ url: "/pages/oaupload/oaupload?url=" + url })
  } else {
    switch (fileTyp) {
      case "jpg":
      case "doc":
      case "docx":
      case "xls":
      case "xlsx":
      case "ppt":
      case "pptx":
        window.open(
          "https://view.officeapps.live.com/op/view.aspx?src=" +
            encodeURIComponent(url)
        )
      default:
        window.open(url, "_self")
    }
  }
}

/*
 *调用软键盘的时候自动居中被激活的元素到可视窗口
 */
export const utils_scrollToView = () => {
  const oldHeight = window.innerHeight
  window.addEventListener("resize", () => {
    setTimeout(() => {
      document.activeElement.scrollIntoViewIfNeeded(true)
    }, 50)
  })
}

/*
 *调过滤显示文件格式图标
 */
export const utils_fileIcon = (type = "") => {
  type = type.toLowerCase()
  switch (type) {
    case "png":
    case "jpg":
      return "ui-icon__img"
    case "doc":
    case "docx":
      return "ui-icon__word"
    case "ppt":
    case "pptx":
      return "iconPPT"
    case "xls":
    case "xlsx":
      return "ui-icon__excel"
    case "txt":
      return "ui-icon__txt"
    case "rar":
    case "zip":
      return "ui-icon__yasuo"
    case "pdf":
      return "ui-icon__pdf"
    default:
      return "ui-icon__other"
  }
}

/*
 *调过滤显示文件格式后缀
 */
export const utils_fileType = (type = "") => {
  type = type.toLowerCase()
  switch (type) {
    case "png":
      return "png"
      break
    case "jpg":
      return "jpg"
      break
    case "doc":
      return "doc"
      break
    case "docx":
      return "docx"
      break
    case "xls":
      return "xls"
      break
    case "xlsx":
      return "xlsx"
      break
    case "ppt":
      return "ppt"
      break
    case "pptx":
      return "pptx"
      break
    case "txt":
      return "txt"
      break
    case "rar":
      return "rar"
      break
    case "zip":
      return "zip"
      break
    case "pdf":
      return "pdf"
      break
    default:
      return "其他"
  }
}

/*
 *调过滤显示文件格式后缀类型
 */
export const utils_fileSubmitTyp = (type = "") => {
  switch (type) {
    case "xxjd":
      return "形象进度"
      break
    default:
      return "其他"
  }
}

/*
 *获取本地存储的数据
 */
export const utils_getLocal = (name = "") => {
  return localStorage.getItem(name)
}

/**
 * 生产派单id
 * @returns {string}
 */
export function randomStr(len = 32) {
  const $chars =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  const maxPos = $chars.length
  let str = ""
  for (let i = 0; i < len; i++) {
    str += $chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return str
}
